package com.android.car_bluetooth_audio.ble.service;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;


import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.android.car_bluetooth_audio.ble.BleManager;
import com.android.car_bluetooth_audio.ble.VibrationBleManager;
import com.android.car_bluetooth_audio.ble.VibrationManagerCallbacks;

public class VibrationBleProfileService extends BleProfileService implements VibrationManagerCallbacks {

    public static final String BROADCAST_MEASUREMENT = "BROADCAST_MEASUREMENT";
//    public static final String BROADCAST_POWER="BROADCAST_POWER";
    public static final String BROADCAST_DEVICE="BROADCAST_DEVICE";
    public static final String BROADCAST_SYSC_DATA="BROADCAST_SYSC_DATA";
    public static final String BROADCAST_SWITCH_DATA="BROADCAST_SWITCH_DATA";
    public static final String BROADCAST_EQ_DATA="BROADCAST_EQ_DATA";
    public static final String BROADCAST_RESET_DATA="BROADCAST_RESET_DATA";
    public static final String BROADCAST_SAVE_DATA="BROADCAST_SAVE_DATA";
    public static final String BROADCAST_RESET_0OR180_DATA="BROADCAST_RESET_0OR180_DATA";
    public static final String BROADCAST_DEVICE_HZ="BROADCAST_DEVICE_HZ";
    public static final String BROADCAST_DEVICE_SUBMIT="BROADCAST_DEVICE_SUBMIT";
    public static final String BROADCAST_CHANNEL_SELECT = "BROADCAST_CHANNEL_SELECT";
    public static final String BROADCAST_DEVICE_DATA_WRITE = "BROADCAST_DEVICE_DATA_WRITE";
    public static final String BROADCAST_UPDATE_DATA = "BROADCAST_UPDATE_DATA";
    public static final String BROADCAST_DEVICE_ON = "BROADCAST_DEVICE_ON";
    public static final String BROADCAST_READ_DATA = "BROADCAST_READ_DATA";
    public static final String BROADCAST_READ_2048_0_SUCCESS = "BROADCAST_READ_2048_0_SUCCESS";
    public static final String BROADCAST_READ_2048_1_SUCCESS = "BROADCAST_READ_2048_1_SUCCESS";
    public static final String BROADCAST_READ_2048_2_SUCCESS = "BROADCAST_READ_2048_2_SUCCESS";

    public static final String EXTRA_TEMPERATURE = "EXTRA_TEMPERATURE";
    public final static String ACTION_DISCONNECT = "ACTION_DISCONNECT";
    private final static int NOTIFICATION_ID = 267;
    private final static int OPEN_ACTIVITY_REQ = 0;
    private final static int DISCONNECT_REQ = 1;
    public static final String BROADCAST_CAPTURE_MEASUREMENT = "BROADCAST_CAPTURE_MEASUREMENT";
    public static final String BROADCAST_CAPTURE_POWER="BROADCAST_CAPTURE_POWER";
    public static final String BROADCAST_CAPTURE_CHANNEL_SELECT = "BROADCAST_CAPTURE_CHANNEL_SELECT";
    public static final String BROADCAST_CAPTUR_UPDATE_DATA = "BROADCAST_CAPTUR_UPDATE_DATA";
    public static final String BROADCAST_CAPTUR_READ_DATA = "BROADCAST_CAPTUR_READ_DATA";
    public static final String BROADCAST_CAPTUR_READ_2048_0_SUCCESS = "BROADCAST_CAPTUR_READ_2048_0_SUCCESS";
    public static final String BROADCAST_CAPTUR_READ_2048_1_SUCCESS = "BROADCAST_CAPTUR_READ_2048_1_SUCCESS";
    public static final String BROADCAST_CAPTUR_READ_2048_2_SUCCESS = "BROADCAST_CAPTUR_READ_2048_2_SUCCESS";
    private VibrationBleManager mManager;

    private final LocalBinder mBinder = new VibrationBinder();



    /**
     * This local binder is an interface for the bonded activity to operate with the HTS sensor
     */
    public class VibrationBinder extends LocalBinder {

//        public void sendCharacteristicData(byte[] data,int type) {
//            if(mManager != null)
//                mManager.sendCharacteristicData(data,type);
//        }

        public void sendCharacteristicData(byte[] data,int type) {
            if(mManager != null)
                mManager.sendCharacteristicData(data,type);
        }

        public void sendSwitch(byte[] data,int type){
            if(mManager != null)
                mManager.sendSwitch(data);
        }

        public void send0Or180(byte[] data,int type){
            if(mManager != null)
                mManager.send0Or180(data);
        }

        public void syncData(byte[] data){
            if(mManager != null)
                mManager.syncData(data);
        }

        public void sendEQMode(byte[] data){
            if(mManager != null)
                mManager.sendEQMode(data);
        }

        public void sendVolum(byte[] data){
            if(mManager != null)
                mManager.sendVolum(data);
        }

        public void sendSave(byte[] data){
            if(mManager != null)
                mManager.sendSave(data);
        }

        public void sendBoost(byte[] data){
            if(mManager != null)
                mManager.sendBoost(data);
        }

        public void sendRest(byte[] data){
            if(mManager != null)
                mManager.sendRest(data);
        }

        public void sendSlideData(byte[] data){
            if(mManager != null)
                mManager.sendSlide(data);
        }

    }

    @Override
    protected LocalBinder getBinder() {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("进入喝了","连接");
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_DISCONNECT);
        LocalBroadcastManager.getInstance(this).registerReceiver(mDisconnectActionBroadcastReceiver, filter);
//        registerReceiver(mDisconnectActionBroadcastReceiver, filter);
    }

    @Override
    protected BleManager initializeManager() {
        return mManager = new VibrationBleManager(this);
    }


    @Override
    public void onDestroy() {
        // when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mDisconnectActionBroadcastReceiver);

        super.onDestroy();
    }

    /**
     * This broadcast receiver listens for {@link #ACTION_DISCONNECT} that may be fired by pressing Disconnect action button on the notification.
     */
    private final BroadcastReceiver mDisconnectActionBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            Log.e("是否连接了", String.valueOf(isConnected()));
            if (isConnected())
                getBinder().disconnect();
            else
                stopSelf();
        }
    };


    @Override
    public void onValueReceived(BluetoothDevice device, byte[] value,int type) {
        Intent broadcast = new Intent();
        Log.e("tyue","type"+type);
        if (type==0){
            broadcast.setAction(BROADCAST_MEASUREMENT);
        }else if (type==1){
            broadcast.setAction(BROADCAST_CAPTURE_MEASUREMENT);
        }

        broadcast.putExtra(EXTRA_DEVICE, device);
        broadcast.putExtra(EXTRA_TEMPERATURE, value);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void onChannelSelectSuccess(int type) {
        Log.e("onChannelSelect","发送");

        Intent broadcast = new Intent();
        if (type==0){
            broadcast.setAction(BROADCAST_CHANNEL_SELECT);
        }else if (type==1){
            broadcast.setAction(BROADCAST_CAPTURE_CHANNEL_SELECT);
        }
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void onUpdateDataToBlueTooth(int type) {
        Log.e("UpdateDataToBlueTooth","发送");
        Intent broadcast = new Intent();
        if (type==0){
            broadcast.setAction(BROADCAST_UPDATE_DATA);
        }else if (type==1){
            broadcast.setAction(BROADCAST_CAPTUR_UPDATE_DATA);
        }
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void onDeviceOn() {
        Intent broadcast = new Intent();
        broadcast.setAction(BROADCAST_DEVICE_ON);

        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void ReadTimeZoneStatus(byte[] value,int type) {

        Intent broadcast = new Intent();
        if (type==0){
            broadcast.setAction(BROADCAST_READ_DATA);
        }else if (type==1){
            broadcast.setAction(BROADCAST_CAPTUR_READ_DATA);
        }
        broadcast.putExtra(EXTRA_TEMPERATURE, value);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void Read2048_0(byte[] value, int off,int type) {
        Intent broadcast = new Intent();
        if (type==0){
            broadcast.setAction(BROADCAST_READ_2048_0_SUCCESS);
        }else if (type==1){
            broadcast.setAction(BROADCAST_CAPTUR_READ_2048_0_SUCCESS);
        }
        broadcast.putExtra(EXTRA_OFF, off);
        broadcast.putExtra(EXTRA_TEMPERATURE, value);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void Read2048_1(byte[] value, int off,int type) {

        Intent broadcast = new Intent();
        if (type==0){
            broadcast.setAction(BROADCAST_READ_2048_1_SUCCESS);
        }else if (type==1){
            broadcast.setAction(BROADCAST_CAPTUR_READ_2048_1_SUCCESS);
        }
        broadcast.putExtra(EXTRA_OFF, off);
        broadcast.putExtra(EXTRA_TEMPERATURE, value);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void Read2048_2(byte[] value, int off,int type) {
        Intent broadcast = new Intent();
        if (type==0){
            broadcast.setAction(BROADCAST_READ_2048_2_SUCCESS);
        }else if (type==1){
            broadcast.setAction(BROADCAST_CAPTUR_READ_2048_2_SUCCESS);
        }
        broadcast.putExtra(EXTRA_OFF, off);
        broadcast.putExtra(EXTRA_TEMPERATURE, value);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void getPowerData(byte[] value,int type) {
        Intent broadcast = new Intent();
//        if (type==0){
//            broadcast.setAction(BROADCAST_POWER);
//        }else if (type==1){
//            broadcast.setAction(BROADCAST_CAPTURE_POWER);
//        }
        broadcast.putExtra(EXTRA_TEMPERATURE, value);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void getDeviceData(byte[] value) {
        Intent broadcast = new Intent(BROADCAST_DEVICE);
        broadcast.putExtra(EXTRA_TEMPERATURE, value);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    //同步数据
    @Override
    public void syscData(String[] datas) {
        Intent broadcast = new Intent(BROADCAST_SYSC_DATA);
        broadcast.putExtra(EXTRA_TEMPERATURE, datas);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void switchData(String data) {
        Intent broadcast = new Intent(BROADCAST_SWITCH_DATA);
        broadcast.putExtra(EXTRA_TEMPERATURE, data);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void eqMode(String data) {
        Intent broadcast = new Intent(BROADCAST_EQ_DATA);
        broadcast.putExtra(EXTRA_TEMPERATURE, data);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);


    }

    @Override
    public void resetMode(String data) {
        Intent broadcast = new Intent(BROADCAST_RESET_DATA);
        broadcast.putExtra(EXTRA_TEMPERATURE, data);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void saveMode(String data) {
        Intent broadcast = new Intent(BROADCAST_SAVE_DATA);
        broadcast.putExtra(EXTRA_TEMPERATURE, data);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void result0Or180(String data) {
        Intent broadcast = new Intent(BROADCAST_RESET_0OR180_DATA);
        broadcast.putExtra(EXTRA_TEMPERATURE, data);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }


    @Override
    public void getDeviceHzData(byte[] value) {
//        Intent broadcast = new Intent(BROADCAST_DEVICE_HZ);
//        broadcast.putExtra(EXTRA_TEMPERATURE, value);
//        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void deviceDataWrite(byte[] value) {
//        Intent broadcast = new Intent(BROADCAST_DEVICE_DATA_WRITE);
//        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }

    @Override
    public void deviceDataSubmit(byte[] value) {
        //BROADCAST_DEVICE_SUBMIT
        Log.e("广播提交","是的体积");
        Intent broadcast = new Intent(BROADCAST_DEVICE_SUBMIT);
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
    }
}
